package com.jyxd.web.service.assessment.basic;


import com.jyxd.web.dao.basic.InOutAmountDao;
import com.jyxd.web.data.basic.InOutAmount;
import com.jyxd.web.data.dto.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;

@Service
@Transactional
public class InOutAmountService {

    @Autowired
    private InOutAmountDao inOutAmountDao;

    public boolean insert(InOutAmount inOutAmount) {
        return inOutAmountDao.insert(inOutAmount);
    }

    public boolean update(InOutAmount inOutAmount) {
        return inOutAmountDao.update(inOutAmount);
    }

    public InOutAmount queryData(String id) {
        return inOutAmountDao.queryData(id);
    }

    public List<InOutAmount> queryList(Map<String, Object> map) {
        return inOutAmountDao.queryList(map);
    }

    public int queryNum(Map<String, Object> map) {
        return inOutAmountDao.queryNum(map);
    }

    /**
     * 病人管理-护理文书-护理单文书-出入量-根据条件查询出入量列表
     *
     * @param map
     * @return list
     */
    public List<Map<String, Object>> getInOutAmountList(Map<String, Object> map) {
        return inOutAmountDao.getInOutAmountList(map);
    }

    /**
     * 病人管理-护理文书-护理单文书-出入量-根据条件查询出入量列表数量
     *
     * @param map
     * @return
     */
    public int getInOutAmountNum(Map<String, Object> map) {
        return inOutAmountDao.getInOutAmountNum(map);
    }

    /**
     * 病人管理-护理文书-护理单文书-出入量-根据条件查询出入量列表(新的直接查10个字段)
     *
     * @param map
     * @return list
     */
    public List<Map<String, Object>> queryInOutAmountList(Map<String, Object> map) {
        return inOutAmountDao.queryInOutAmountList(map);
    }

    public HttpResult queryVitalSignData(Map<String, Object> map) {
        errorIfEmpty(map, "请求参数不能为空");
        Integer start = null;
        Integer totalCount = null;
        if (map != null && map.containsKey("start")) {
            totalCount = getInOutAmountNum(map);
            start = ((int) map.get("start") - 1) * (int) map.get("size");
            map.put("start", start);
        }
        List<Map<String, Object>> amountList = queryInOutAmountList(map);
        if (CollectionUtils.isEmpty(amountList)) {
            return new HttpResult(200, "暂无数据");
        }
        return new HttpResult(200, amountList, start, totalCount, "查询数据成功");
    }

    /**
     * 根据医嘱主键查询余量，从大到小排序
     *
     * @param map
     * @return
     */
    public List<String> getAllowanceDosageByOrderNo(Map<String, Object> map) {
        return inOutAmountDao.getAllowanceDosageByOrderNo(map);
    }

    public Map<String, Object> queryInOutAmountById(Map<String, Object> map) {
        return inOutAmountDao.queryInOutAmountById(map);
    }

    /**
     * 通过患者id和时间类表查询总结的数据
     *
     * @param map
     */
    public List<InOutAmount> getTemplateInOutData(Map<String, Object> map) {

        return inOutAmountDao.getTemplateInOutData(map);
    }

    public HttpResult updateUseSign(Map<String, Object> map) {
        try {
            errorIfEmpty(map, "参数不能为空");
            errorIfEmpty(map.get("id"), "数据id不能为空");
            errorIfEmpty(map.get("useSign"), "数据类型不能为空");
            InOutAmount inOutAmount = inOutAmountDao.queryData((String) map.get("id"));
            errorIfEmpty(inOutAmount, "出入量数据不存在");
            inOutAmount.setUseSign((String) map.get("useSign"));
            inOutAmountDao.update(inOutAmount);
            return new HttpResult(200, "标识数据修改成功");
        } catch (Exception e) {
            return new HttpResult(500, e.getMessage());
        }
    }

    /**
     * 查询是否可以进行修改出入量数据
     *
     * @param map
     * @return
     */
    public HttpResult isUpdateData(Map<String, Object> map) {
        try {
            errorIfEmpty(map, "参数不能为空");
            errorIfEmpty(map.get("parent_id"), "父级id不能为空");
            int count = inOutAmountDao.queryCountByParentId(map);
            if (count > 0)
                return new HttpResult(200, "false");
            return new HttpResult(200, "true");
        } catch (Exception e) {
            return new HttpResult(500, e.getMessage());
        }
    }
}
